﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Caching;
using System.Text;
using System.Threading.Tasks;

namespace Shared.Cache
{
    public class MemoryCacheStorage : ICacheStorage
    {
        protected MemoryCache cache;
        public MemoryCacheStorage()
        {            
            this.cache = new MemoryCache("app_cache");
        }

        public void Remove(string key)
        {
            cache.Remove(key);
        }

        public void Set(string key, object data)
        {
            cache.Add(key, data, DateTimeOffset.Now.AddMinutes(30));
        }

        public void Set(string key, object data, TimeSpan timeSpan)
        {
            CacheItemPolicy policy = new CacheItemPolicy();
            policy.SlidingExpiration = timeSpan;
            cache.Add(key, data, policy);
        }

        public T Get<T>(string storageKey)
        {
            return (T)cache.Get(storageKey);
        }

        public IEnumerable<KeyValuePair<string, object>> GetAllEntries()
        {
            return cache.ToList();
        }
    }
}
